/*
 * HackSQL
 * Copyright (C) 2008-2009
 *
 * Authors: Higor Eurípedes Pimentel Fernandes de Araújo
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * $Id: utils.c 30 2009-03-03 23:15:18Z felipensp $
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>

#include <sys/stat.h>


#include <readline/readline.h>
#include <readline/history.h>

/* headers pra conectar no bagui */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>

#include "types.h"
#include "utils.h"

inline void *get_in_addr(struct sockaddr *sa)
{
	if (sa->sa_family == AF_INET) {
		return &(((struct sockaddr_in *) sa)->sin_addr);
	}

	return &(((struct sockaddr_in6 *) sa)->sin6_addr);
}

char *trimstr(char *string)
{
	char *s, *t;

	for (s = string; whitespace(*s); s++);

	if (*s == 0)
		return (s);

	t = s + strlen(s) - 1;
	while (t > s && whitespace(*t))
		t--;
	*++t = '\0';

	return s;
}

char **tokenize(char *str, char *delimiters, int *num)
{
	char **toks = NULL;
	char *token;
	int count = 0;
	char *buf = strdup(str);

	token = strtok(buf, delimiters);

	while (token != NULL) {
		toks = (char **) realloc(toks, sizeof(char *) * count + 1);
		toks[count++] = token;

		token = strtok(NULL, delimiters);
	}

	num = &count;
	if (!count)
		return NULL;

	toks[count] = NULL;

	return toks;
}

long int getfilesize(char *filename)
{
	struct stat s;

	if (stat(filename, &s) < 0)
		return -1;

	return (long int) s.st_size;
}

char *getfilecontents(char *filename)
{
	FILE *fp;
	char *buf;
	long int size;

	if (!(fp = fopen(filename, "r")))
		return NULL;

	if ((size = getfilesize(filename)) < 0)
		return NULL;

	buf = (char *) malloc(sizeof(char) * size);

	if (!fread(buf, 1, size, fp)) {
		free(buf);
		buf = NULL;
	}

	fclose(fp);
	return buf;
}

void hs_error(hs_errno_t errno, char *format, ...)
{
	va_list valist;

	if (format == NULL) {
		fprintf(stderr, "HackSQL Error: Unknown.\n");
		return;
	}

	va_start(valist, format);

	if (!errno)
		sprintf(format, "HackSQL Error (#0): %s\n", format);
	else
		sprintf(format, "HackSQL Error (#%i): %s\n", errno, format);

	vfprintf(stderr, format, valist);
	va_end(valist);
}

void hs_warning(char *format, ...)
{
	va_list valist;

	hs_return_if_null(format);

	va_start(valist, format);

	sprintf(format, "HackSQL Warning: %s\n", format);

	vfprintf(stderr, format, valist);
	va_end(valist);
}
